package com.pearadmin.pro.modules.gen.rest;


import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.web.base.BaseController;

import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.gen.domain.GenTable;
import com.pearadmin.pro.modules.gen.domain.GenTableColumn;
import com.pearadmin.pro.modules.gen.param.GenTableImportRequest;
import com.pearadmin.pro.modules.gen.param.TableRequest;
import com.pearadmin.pro.modules.gen.service.GenTableColumnService;
import com.pearadmin.pro.modules.gen.service.GenTableService;
import io.swagger.annotations.Api;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Api(tags = {"代码生成"})
@RestController
@RequestMapping(ControllerConstant.PREFIX + "gen")
public class GenController extends BaseController {


    @Resource
    private GenTableService genTableService;

    @Resource
    GenTableColumnService genTableColumnService;



    /**
     * 查询代码生成列表
     *
     * @param request 查询参数
     */
    @GetMapping("/page")
    @ResponseBody
    public Result page(TableRequest request) {
        return success(genTableService.selectGenTableList(request));
    }

    /**
     * 查询数据库列表
     *
     * @param request 查询参数
     */
    @GetMapping("/db/page")
    @ResponseBody
    public Result DBPage(TableRequest request) {
        return success(genTableService.selectDbTableList(request));
    }


    /**
     * 导入表结构（保存）
     */
    @ResponseBody
    @PostMapping("/import/table")
    public Result importTable(@RequestBody GenTableImportRequest request) {
        String[] tableNames = request.getTableNames();
        List<GenTable> tableList = genTableService.selectDbTableListByNames(tableNames);
        genTableService.importGenTable(tableList);
        return success("导入成功");

    }


    /**
     * 查询数据表字段列表
     */
    @GetMapping("/column/list")
    @ResponseBody
    public Result columnList(GenTableColumn genTableColumn) {
        List<GenTableColumn> list = genTableColumnService.selectGenTableColumnListByTableId(genTableColumn);
        return success(list);
    }

    /**
     * 修改保存代码生成业务
     *
     * @param genTable 代码生成实体
     */
    @PostMapping("/edit")
    @ResponseBody
    public Result editSave( @RequestBody @Validated GenTable genTable) {
        System.out.println("genTable = " + genTable);
        System.out.println("genTable = " + genTable.getTableId());
        genTableService.validateEdit(genTable);
        genTableService.updateGenTable(genTable);
        return success("保存成功");
    }



    /**
     * 预览代码
     *
     * @param tableId 表格编号
     */
    @GetMapping("/preview/{tableId}")
    @ResponseBody
    public Result preview(@PathVariable("tableId") String tableId) throws IOException {
        Map<String, String> dataMap = genTableService.previewCode(tableId);
        return success(dataMap);
    }



}
